home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 551 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  54 lines

  1. Path: news.ifu.net!news
  2. From: Jason Gresh <gresh@ifu.net>
  3. Newsgroups: comp.lang.c++
  4. Subject: Creating a pointer to a function "void (*ptrFunction)()"  inside a class
  5. Date: Thu, 04 Jan 1996 22:54:55 -0500
  6. Organization: Internet For 'U'
  7. Message-ID: <30ECA10F.3D99@ifu.net>
  8. NNTP-Posting-Host: ip21.ifu.net
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b3 (Win95; I)
  13.  
  14. Hi,
  15.  
  16.     I am trying to create a base class that has a function that the 
  17. derived class needs to create.  In order to do this, I want to create a 
  18. pointer to a function in the base class that the derived class can then 
  19. define.  This is not a case where an override will work because the 
  20. number and names of the derived functions is not known.  Hopefully this 
  21. code sample will make this clearer:
  22.  
  23. class BaseClass
  24. {
  25.     int (*ptrFunction)();
  26. }
  27.  
  28. class DerivedClass : BaseClass
  29. {
  30.     DerivedClass::DerivedClass();
  31.     int myFunction(int, int);
  32. }
  33.  
  34. DerivedClass::DerivedClass()
  35. {
  36.     ptrFunction = myFunction;
  37. }
  38.  
  39. DerivedClass::myFunction(int, int)
  40. {
  41. // code ....
  42. }
  43.  
  44. If I don't make myFunction part of the derived class (global), 
  45. everything works fine.  As soon as I include it in the class, I cannot 
  46. assign a pointer to it.  I am aware that pointers to functions inside 
  47. the class include the class name in some way ( this is not an issue for 
  48. global functions).  What is the casting (or other) mechanism to make
  49. this work?
  50.  
  51. Thanks,
  52.  
  53.     Mike Gresh
  54.